package com.ngc.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ngc.mall.common.configure.mybatisPlus.BaseServiceImpl;
import com.ngc.mall.core.enumeration.PlatformType;
import com.ngc.mall.domain.WxUserDomain;
import com.ngc.mall.dto.MemberFilterDTO;
import com.ngc.mall.mapper.WxUserMapper;
import com.ngc.mall.service.IWxUserService;
import com.ngc.mall.vo.MemberOverviewVO;
import com.ngc.mall.vo.PagingVO;
import com.ngc.mall.vo.WxUserVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.List;
@Service
public class WxUserServiceImpl extends BaseServiceImpl<WxUserMapper, WxUserDomain> implements IWxUserService {

    @Override
    public List<WxUserDomain> selectAllWxUser(){
        return super.list();
    }

    @Override
    public PagingVO<WxUserDomain> search(MemberFilterDTO dto, int start, int count) {
        Page<WxUserDomain> page=new Page<>(start,count);
        LambdaQueryWrapper<WxUserDomain> queryWrapper= Wrappers.<WxUserDomain>lambdaQuery().orderByDesc(WxUserDomain::getCreateTime);
        if(StringUtils.isNotBlank(dto.getNickname())){
            queryWrapper.like(WxUserDomain::getNickname,dto.getNickname());
        }
        if(dto.getSourceType()!= PlatformType.ALL.getValue()){
            queryWrapper.eq(WxUserDomain::getSource,dto.getSourceType());
        }
//        if(dto.getLevel()!= 0){
//            queryWrapper.eq(WxUserDomain::getLevel,dto.getLevel());
//        }
        IPage iPage=super.page(page,queryWrapper);
        PagingVO<WxUserDomain> pagingVO=new PagingVO<>(iPage);
        return pagingVO;
    }

    @Override
    public MemberOverviewVO getMemberOverview() {
        // 创建一个Calendar实例
        Calendar calendar = Calendar.getInstance();

        // 获取当前日期
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        // 获取昨天的年、月、日
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        LocalDateTime now=LocalDateTime.now();
        LocalDateTime yesterday= LocalDateTime.of(year,month,day,23,59,59);

        int todayTotalMember=super.count(Wrappers.<WxUserDomain>lambdaQuery().le(WxUserDomain::getCreateTime,now));
        int lastTotalMember=super.count(Wrappers.<WxUserDomain>lambdaQuery().le(WxUserDomain::getCreateTime,yesterday));
        int rate=todayTotalMember-lastTotalMember;
        return MemberOverviewVO.builder().totalMember(todayTotalMember).lastTotalMember(lastTotalMember).today(now).rate(rate).yesterday(yesterday).build();
    }

    @Override
    public WxUserVO getDetailById(Long id) {
        WxUserDomain domain=super.getById(id);
        return WxUserVO.builder().nickname(domain.getNickname()).source(domain.getSource()).createTime(domain.getCreateTime()).annualEnd(domain.getAnnualEnd()).annualStart(domain.getAnnualStart()).levelId(domain.getLevelId()).mobilePhone(domain.getMobilePhone()).profilePhoto(domain.getProfilePhoto()).build();
    }


}
